﻿using ServiceCore.DbContext;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;
using ServiceCore.Entity.Param.Tag;
using ServiceCore.Utility.Extension;
using System.Configuration;

namespace ServiceCore.Service
{
    /// <summary>
    /// 标签类型服务
    /// </summary>
    public partial class TagTypeService : SqlsugarServices<TagTypeInfo>
    {
        /// <summary>
        /// 分页 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<TagTypeInfo, List<TagTypeInfo>> TagType_List(BaseParam<TagTypeInfo, List<TagTypeInfo>> arg)
        {
            arg.Data = this.Context.Queryable<TagTypeInfo>()
                .OrderBy(x => x.CreateTime)
                .ToList();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<TagTypeInfo, TagTypeInfo> TagType_Exists(BaseParam<TagTypeInfo, TagTypeInfo> arg, bool? isAdd = null)
        {
            arg.Data = this.Context.Queryable<TagTypeInfo>()
                .WhereIF(arg.Id > 0, x => x.Id == arg.Id)
                .WhereIF(arg.Param != null && string.IsNullOrEmpty(arg.Param.Name) == false, x => x.Name == arg.Param.Name)
                .WhereIF(isAdd.HasValue && isAdd == false, x => x.Id != arg.Param.Id)
                .First();

            return arg;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<TagTypeInfo, long> TagType_Add(BaseParam<TagTypeInfo, long> arg)
        {
            arg = TagType_Add_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = InsertReturnBigIdentity(arg.Param);
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<TagTypeInfo, long> TagType_Edit(BaseParam<TagTypeInfo, long> arg)
        {
            arg = TagType_Edit_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Updateable<TagTypeInfo>()
                .SetColumns(x => new TagTypeInfo() { Name = arg.Param.Name })
                .Where(x => x.Id == arg.Param.Id)
                .ExecuteCommand();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<TagDeleteParam, long> TagType_Delete(BaseParam<TagDeleteParam, long> arg)
        {
            arg = TagType_Del_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            this.Context.Deleteable<RecordInfo>().Where(x => arg.Param.TagIds.Contains(x.TagId)).ExecuteCommand();
            this.Context.Deleteable<TagInfo>().Where(x => arg.Param.TagIds.Contains(x.Id)).ExecuteCommand();
            arg.Data = this.Context.Deleteable<TagTypeInfo>().Where(x => x.Id == arg.Id).ExecuteCommand();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="confData"></param>
        /// <returns></returns>
        public BaseParam<string, string> InitData(string confData)
        {
            if (!string.IsNullOrWhiteSpace(confData))
            {
                var list = confData.ToObject<List<InitConf>>();
                var nowTime = DateTime.Now;
                var zodiacName = GetZodiacName();
                foreach (var item in list)
                {
                    if (string.IsNullOrWhiteSpace(item.Name))
                    {
                        continue;
                    }
                    var tagTypeInfo = this.Context.Queryable<TagTypeInfo>().Where(x => x.Name == item.Name).First();
                    if (tagTypeInfo == null)
                    {
                        tagTypeInfo = new TagTypeInfo { Name = item.Name, CreateUserId = 1, CreateTime = nowTime };
                        tagTypeInfo.Id = this.Context.Insertable(tagTypeInfo).ExecuteReturnIdentity();
                    }
                    if (zodiacName == item.Name)
                    {
                        item?.List.Append("25");
                    }
                    if (item?.List?.Any() == false)
                    {
                        continue;
                    }
                    foreach (var tagName in item.List)
                    {
                        var tagInfo = this.Context.Queryable<TagInfo>().Where(x => x.Name == tagName && x.TagTypeId == tagTypeInfo.Id).First();
                        if (tagInfo == null)
                        {
                            tagInfo = new TagInfo { Name = tagName, TagTypeId = tagTypeInfo.Id, CreateUserId = 1, CreateTime = nowTime };
                            this.Context.Insertable(tagInfo).ExecuteCommand();
                        }
                    }
                }
            }
            return new BaseParam<string, string> { ErrorMsg = "" };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetZodiacName()
        {
            var listZodiac = new List<string>() { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
            var year = DateTime.Now.Year;
            var index = (year - 1900) % 12;
            return listZodiac[index];
        }
    }
}
